package com.xctech.yace.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.java.tuple.Tuple2;

import java.util.HashMap;
import java.util.Map;

public class JSONUtil {

//    public static void main(String[] args) {
//        String s = "{\n" +
//                "    \"ORDE_SCR_CTRL_TYPE\":2,\n" +
//                "    \"ORDE_SCR_REAL\":\n" +
//                "    [\n" +
//                "        {\n" +
//                "            \"SCR_CODE\":\"300002\",\n" +
//                "            \"PD_ID\":106,\n" +
//                "            \"BGNG_HAS_ENTR_AMT\":null,\n" +
//                "            \"ACC_ID\":225,\n" +
//                "            \"AST_UNIT_ID\":124,\n" +
//                "            \"MKT_NO\":2,\n" +
//                "            \"BUSI_CLAS\":\"050101\",\n" +
//                "            \"ENTR_DIR\":12,\n" +
//                "            \"IN_ORDE_VOL\":null,\n" +
//                "            \"ORDE_STAT\":1,\n" +
//                "            \"FEER\":null,\n" +
//                "            \"IN_AST_UNIT_ID\":null,\n" +
//                "            \"BGNG_HAS_MTCH_AMT\":null,\n" +
//                "            \"RELA_RRSN\":1000,\n" +
//                "            \"BUSI_TYPE\":\"05010100\",\n" +
//                "            \"OPER_TYPE\":0,\n" +
//                "            \"IN_PD_ID\":null,\n" +
//                "            \"AGT_RELA_RRSN\":1001,\n" +
//                "            \"LAS_ID\":\"1\",\n" +
//                "            \"ORDE_PRC\":100.0,\n" +
//                "            \"ORDE_SCR_ONUM\":\"20210622_1_300016_1_300001\",\n" +
//                "            \"IN_ACC_ID\":null,\n" +
//                "            \"IN_ORDE_AMT\":null,\n" +
//                "            \"ORDE_ASSO_ONUM\":\"20210622_1_300016_225_002000300002_2_05010100\",\n" +
//                "            \"MEMO\":null,\n" +
//                "            \"CLR_SPED\":1,\n" +
//                "            \"PFL_ID\":225,\n" +
//                "            \"ORDE_VOL\":100.0,\n" +
//                "            \"IVSM_TYPE\":2,\n" +
//                "            \"CMS\":null,\n" +
//                "            \"ORDE_PRC_TYPE\":1,\n" +
//                "            \"MARG\":150.0,\n" +
//                "            \"DELI_DATE\":20210622,\n" +
//                "            \"DELI_MODE\":null,\n" +
//                "            \"SCR_INCD\":2000300002,\n" +
//                "            \"IN_PFL_ID\":null,\n" +
//                "            \"ORDE_AMT\":10000.0,\n" +
//                "            \"BGNG_HAS_MTCH_VOL\":null,\n" +
//                "            \"IN_IVSM_TYPE\":null,\n" +
//                "            \"BGNG_HAS_ENTR_VOL\":null,\n" +
//                "            \"IN_SCR_INCD\":null,\n" +
//                "            \"IN_ORDE_PRC\":null,\n" +
//                "            \"SHAD_CD\":\"0123456789\",\n" +
//                "            \"CRRC\":156\n" +
//                "        }\n" +
//                "\n" +
//                "    ],\n" +
//                "    \"SYS_ID\":1,\n" +
//                "    \"ORDE_END_TIME\":null,\n" +
//                "    \"TRD_SRC_ID\":\"0|20210622_1_300016|0|0|0\",\n" +
//                "    \"OPER_TIME\":null,\n" +
//                "    \"ORDE_STRT_TIME\":null,\n" +
//                "    \"EVT_ONUM\":20210622100000110001,\n" +
//                "    \"ORDE_END_DATE\":20210622,\n" +
//                "    \"SERV_OPER_TYPE\":201,\n" +
//                "    \"ORDE_STRT_DATE\":20210622,\n" +
//                "    \"ORDE_RELA_DATE\":20210622,\n" +
//                "    \"XID\":\"0x0021.023.000001f71\",\n" +
//                "    \"OCCU_DATE\":20210622,\n" +
//                "    \"ORDE_TYPE\":1,\n" +
//                "    \"ORDE_ONUM\":\"20210622_1_300016_1\",\n" +
//                "    \"OCCU_TIME\":null,\n" +
//                "    \"BUSI_DATE\":20210622\n" +
//                "}";
//        JSONObject jsonObject = JSON.parseObject(s);
//        Map<String, String> map = new HashMap<>();
//        map.put("ORDE_SCR_ONUM","2");
//        map.put("ORDE_ONUM","2");
//        map.put("ORDE_ASSO_ONUM","2");
//        map.put("ENTR_ONUM","3");
//        map.put("WHDW_ENTR_ONUM","3");
//        map.put("MTCH_ONUM","4");
//        map.put("SETT_ONUM","5");
//        conversionField(jsonObject,"ORDE_SCR_REAL",map);
//        System.out.println(jsonObject);
//    }

    /**
     *
     * @param json
     * @param field
     */
    public static void conversionField(JSONObject json, String field,Map<String,String> convers){
        for(Map.Entry<String, String> entry:convers.entrySet()){
            String converField = entry.getKey();
            String append = entry.getValue();
            //外层
            if(json.containsKey(converField)){
                String target = json.getString(converField)+"#"+append;
                json.replace(converField,target);
            }
            //内层
            JSONArray jsonArray = json.getJSONArray(field);
            for(int i =0;i < jsonArray.size();i++){
                JSONObject subJson = (JSONObject)jsonArray.get(i);
                if(subJson.containsKey(converField)){
                    String target = subJson.getString(converField)+"#"+append;
                    subJson.replace(converField,target);
                }
            }
        }
    }


    /**
     * INSERT INTO TBALE
     *      （字段1，字段2，字段3）
     * VALUES
     *      （值1，值2，值3）
     *
     * @param jsonObj
     * @return
     */
    public static String gensql(String tableName,JSONObject jsonObj){
        String sql = null;
        StringBuilder fields = new StringBuilder();
        StringBuilder values = new StringBuilder();

        fields.append("(");
        values.append("(");

        for(Map.Entry<String, Object> entry:jsonObj.entrySet()){
            String field = entry.getKey();
            Object value = entry.getValue();
            if(value instanceof JSONArray){
                JSONArray arrays = (JSONArray)value;
                for(int i = 0;i < arrays.size();i++){
                    JSONObject o = (JSONObject)arrays.get(i);
                    for(Map.Entry<String, Object> en:o.entrySet()){
                        String key = en.getKey();
                        Object value1 = en.getValue();
                        if(!(value1 instanceof JSONArray)){
                            fields.append(key);
                            if(value1 instanceof String){
                                values.append("'");
                            }
                            values.append(value1);
                            if(value1 instanceof String){
                                values.append("'");
                            }
                        }
                        fields.append(",");
                        values.append(",");
                    }
                }
            }
            else{
                fields.append(field);
                if(value instanceof String){
                    values.append("'");
                }
                values.append(value);
                if(value instanceof String){
                    values.append("'");
                }
                fields.append(",");
                values.append(",");
            }
        }

        fields.deleteCharAt(fields.length()-1);
        values.deleteCharAt(values.length()-1);
        fields.append(")");
        values.append(")");

        sql = "INSERT INTO "+tableName+"\n"+fields.toString()+"\n"+"VALUES \n"+
                values.toString();
        return sql;
    }

    public static void main(String[] args) {
        JSONObject jsonObject = JSONObject.parseObject("{\n" +
                "    \"XID\": \"cunk01\",\n" +
                "    \"EVT_ONUM\": 20210115100000010001,\n" +
                "    \"SYS_ID\": 1,\n" +
                "    \"TRD_SRC_ID\": \"0|20210115_1_2001601|0|0|0\",\n" +
                "    \"SERV_OPER_TYPE\": 201,\n" +
                "    \"BUSI_DATE\": 20210115,\n" +
                "    \"OCCU_DATE\": 20210115,\n" +
                "    \"OCCU_TIME\": null,\n" +
                "    \"OPER_TIME\": null,\n" +
                "    \"ORDE_ONUM\": \"20210115_1_2001601_1\",\n" +
                "    \"ORDE_TYPE\": 1,\n" +
                "    \"ORDE_RELA_DATE\": 20210115,\n" +
                "    \"ORDE_STRT_DATE\": 20210115,\n" +
                "    \"ORDE_END_DATE\": 20210116,\n" +
                "    \"ORDE_STRT_TIME\": null,\n" +
                "    \"ORDE_END_TIME\": null,\n" +
                "    \"ORDE_SCR_CTRL_TYPE\": 1,\n" +
                "    \"ORDE_SCR_REAL\": [\n" +
                "        {\n" +
                "            \"ORDE_SCR_ONUM\": \"20210115_1_2001601_1_2001601\",\n" +
                "            \"ORDE_ASSO_ONUM\": \"20210115_1_2001601_200901_6000890890_2_16000300\",\n" +
                "            \"PD_ID\": 5001,\n" +
                "            \"AST_UNIT_ID\": 69351,\n" +
                "            \"PFL_ID\": 200901,\n" +
                "            \"ACC_ID\": 200901,\n" +
                "            \"IVSM_TYPE\": 2,\n" +
                "            \"BUSI_CLAS\": \"160003\",\n" +
                "            \"BUSI_TYPE\": \"16000300\",\n" +
                "            \"SCR_INCD\": 6000890890,\n" +
                "            \"MKT_NO\": 6,\n" +
                "            \"SCR_CODE\": \"890890\",\n" +
                "            \"SRC_SCR_ID\": null,\n" +
                "            \"SRC_SCR_TRD_ID\": null,\n" +
                "            \"LAS_ID\": \"1\",\n" +
                "            \"CRRC\": 156,\n" +
                "            \"ENTR_DIR\": 16,\n" +
                "            \"ORDE_PRC\": 11.0,\n" +
                "            \"ORDE_VOL\": 10000.0,\n" +
                "            \"ORDE_AMT\": 10000.0,\n" +
                "            \"INTR_AMT\": null,\n" +
                "            \"BGNG_HAS_ENTR_VOL\": null,\n" +
                "            \"BGNG_HAS_ENTR_AMT\": null,\n" +
                "            \"BGNG_HAS_MTCH_VOL\": null,\n" +
                "            \"BGNG_HAS_MTCH_AMT\": null,\n" +
                "            \"TRD_CNTP_ID\": null,\n" +
                "            \"DELI_DATE\": 20210117,\n" +
                "            \"RELA_RRSN\": 1000,\n" +
                "            \"AGT_RELA_RRSN\": 1001,\n" +
                "            \"MEMO\": \"\\u5b58\\u6b3e\\u5b58\\u5165\\u6307\\u4ee4\",\n" +
                "            \"HUGE_REDP_MODE\": null,\n" +
                "            \"BONS_MODE\": null,\n" +
                "            \"OPER_TYPE\": 1,\n" +
                "            \"IN_PD_ID\": null,\n" +
                "            \"IN_AST_UNIT_ID\": null,\n" +
                "            \"IN_PFL_ID\": null,\n" +
                "            \"IN_ACC_ID\": null,\n" +
                "            \"IN_IVSM_TYPE\": null,\n" +
                "            \"IN_SCR_INCD\": null,\n" +
                "            \"IN_SRC_SCR_ID\": null,\n" +
                "            \"IN_ORDE_VOL\": null,\n" +
                "            \"IN_ORDE_PRC\": null,\n" +
                "            \"IN_ORDE_AMT\": null,\n" +
                "            \"ORDE_STAT\": null,\n" +
                "            \"DPSI_AGMT_NUM\": \"hth10085\",\n" +
                "            \"DPSI_TYPE\": 3.0,\n" +
                "            \"DPSI_ACC\": 95580.0,\n" +
                "            \"DPSI_ACC_NAME\": \"\\u4e2d\\u56fd\\u90ae\\u653f\",\n" +
                "            \"DPSI_HEOF_NO\": \"yz95580\",\n" +
                "            \"DPSI_SUBH\": \"\\u4e2d\\u56fd\\u90ae\\u653f\\u676d\\u5dde\\u652f\\u884c\",\n" +
                "            \"DPSI_CITY\": \"\\u676d\\u5dde\",\n" +
                "            \"DPSI_INTR \": 1.0,\n" +
                "            \"DPSI_PAY_DATE\": 20210117.0,\n" +
                "            \"DPSI_SRTE_DATE\": 20210117.0,\n" +
                "            \"DPSI_MATU_DATE\": 20210218.0,\n" +
                "            \"AHED_WHDW_LMT\": 1.0,\n" +
                "            \"NOTY_DAYS\": 1.0,\n" +
                "            \"DEPR_NUM\": 10085.0,\n" +
                "            \"WHDW_TYPE\": 1.0,\n" +
                "            \"NOTY_DATE\": 20210217.0,\n" +
                "            \"WHDW_DATE\": 20210218.0\n" +
                "        }\n" +
                "    ],\n" +
                "    \"ORDE_DPSI_REAL\": [\n" +
                "        {\n" +
                "            \"DPSI_AGMT_NUM\": \"hth10085\",\n" +
                "            \"DPSI_TYPE\": 3.0,\n" +
                "            \"DPSI_ACC\": 95580.0,\n" +
                "            \"DPSI_ACC_NAME\": \"\\u4e2d\\u56fd\\u90ae\\u653f\",\n" +
                "            \"DPSI_HEOF_NO\": \"yz95580\",\n" +
                "            \"DPSI_SUBH\": \"\\u4e2d\\u56fd\\u90ae\\u653f\\u676d\\u5dde\\u652f\\u884c\",\n" +
                "            \"DPSI_CITY\": \"\\u676d\\u5dde\",\n" +
                "            \"DPSI_INTR \": 1.0,\n" +
                "            \"DPSI_PAY_DATE\": 20210117.0,\n" +
                "            \"DPSI_SRTE_DATE\": 20210117.0,\n" +
                "            \"DPSI_MATU_DATE\": 20210218.0,\n" +
                "            \"AHED_WHDW_LMT\": 1.0,\n" +
                "            \"NOTY_DAYS\": 1.0,\n" +
                "            \"DEPR_NUM\": 10085.0,\n" +
                "            \"WHDW_TYPE\": 1.0,\n" +
                "            \"WHDW_DATE\": 20210218.0\n" +
                "        }\n" +
                "    ]\n" +
                "}\n");
//        String gensql = gensql(jsonObject);
//        System.out.println(gensql);
    }
}
